package com.yun.baymax.adapter;

import android.content.DialogInterface;
import android.content.Intent;
import android.support.v7.app.AlertDialog;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseMultiItemQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.chad.library.adapter.base.entity.MultiItemEntity;
import com.yun.baymax.R;
import com.yun.baymax.activity.ItemActivity;
import com.yun.baymax.bean.adapter.ProfileType;
import com.yun.baymax.bean.adapter.Trends;
import com.yun.baymax.utils.GlideUtils;

import java.util.List;

import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.UpdateListener;

/**
 * Created by Administrator on 2017/6/13.
 */

public class TrendsAdapter extends BaseMultiItemQuickAdapter<MultiItemEntity, BaseViewHolder> {
    public static final int HEAD_TYPE = 0;
    public static final int BODY_TYPE = 1;

    public TrendsAdapter(List<MultiItemEntity> data) {
        super(data);
        addItemType(HEAD_TYPE, R.layout.item_discover_head);
        addItemType(BODY_TYPE, R.layout.item_discover_body);
    }


    @Override
    protected void convert(final BaseViewHolder holder, MultiItemEntity entity) {
        switch (holder.getItemViewType()) {
            case HEAD_TYPE:
                final ProfileType type = (ProfileType) entity;
                holder.setImageResource(R.id.iv, type.isExpanded() ? R.drawable.arrow_b : R.drawable.arrow_r)
                        .setImageResource(R.id.iv_icon, type.getIcon());
                holder.setText(R.id.tv_title, type.getTitle());
//                holder.setText(R.id.trend_num,type.getSubItems()==null?"0":String.valueOf(type.getSubItems().size()));
                holder.itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        int pos = holder.getAdapterPosition();
                        if (type.isExpanded()) {
                            collapse(pos);
                        } else {
                            expand(pos);
                        }
                    }
                });

                break;
            case BODY_TYPE:
                final Trends trend = (Trends) entity;
                holder.setText(R.id.tv_people, trend.author.getUserName())
                        .setText(R.id.tv_title, trend.title).setText(R.id.tv_content, trend.content)
                        .setText(R.id.tv_time, trend.getCreatedAt());
                if (trend.pics != null)
                    Glide.with(mContext).load(trend.pics.get(0)).into((ImageView) holder.getView(R.id.iv_img));
                else holder.getView(R.id.iv_img).setVisibility(View.GONE);
                Glide.with(mContext).load(trend.author.getHeadImage()).asBitmap().into(GlideUtils.setCircle(mContext, (ImageView) holder.getView(R.id.iv_avatar)));
                holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
                    @Override
                    public boolean onLongClick(View v) {
                        AlertDialog.Builder dialog = new AlertDialog.Builder(mContext);
                        dialog.setMessage("删除此动态？")
                                .setNegativeButton("手滑了", new DialogInterface.OnClickListener() {
                                    @Override
                                    public void onClick(DialogInterface dialog, int which) {
                                        dialog.dismiss();
                                    }
                                })
                                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                                    @Override
                                    public void onClick(DialogInterface dialog, int which) {
                                        deleteTrend(trend, holder);
                                    }
                                }).show();
                        return true;
                    }
                });
                holder.itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent intent = new Intent(mContext, ItemActivity.class);
                        intent.putExtra("trend", trend);
                        mContext.startActivity(intent);
                    }
                });
                break;
            default:
                break;
        }
    }

    private void deleteTrend(final Trends trend, final BaseViewHolder holder) {
        trend.delete(new UpdateListener() {
            @Override
            public void done(BmobException e) {
                if (e != null)
                    Toast.makeText(mContext, "删除失败，请稍后重试", Toast.LENGTH_SHORT).show();
                else {
                    int pp = getParentPosition(trend);
                    ((ProfileType) getData().get(pp)).removeSubItem(trend);
                    getData().remove(holder.getLayoutPosition());
                    notifyDataSetChanged();

                }
            }
        });
    }
}
